import os
import time

import pyperclip
import pywebio.input
from pywebio.input import input, FLOAT, NUMBER
import pywebio.output as output
from pywebio.output import put_text
import pywebio.pin as pin
from pywebio.session import hold
from pywebio import start_server
import random
vfc = 100000
def tst():
    global vfc
    output.put_markdown("# PyWebIO 无线剪切板 | 文件传输 #")
    vfc = random.randint(100000, 999999)
    print("验证码：" + str(vfc))
    output.put_buttons(['提交数据'], lambda _: cal())
    output.put_buttons(['读取数据'], lambda _: reads())
    output.put_buttons(["清除数据"], lambda _: clear())
    pin.put_input('num1', label="请输入内容")
    pin.put_input('vfc1', label="验证码")
    output.put_buttons(['点击验证'], lambda _: pd())
    def pd():
        global vfc
        if(pin.pin['vfc1'] == str(vfc)):
            output.popup("成功！","验证成功！")
            vfc = random.randint(100000, 999999)
            print("验证码：" + str(vfc))
        else:
            output.popup("失败", "验证失败！验证码错误")
    def clear():
        try:
            os.remove("clipboard.txt")
        except:
            output.popup("Err！", '没有内容！')
        else:
            output.popup("Success！", '清除成功！')
            output.put_markdown(str(time.time()) + "清除！\n\n\n\n\nSuccess！")
    def cal():
        num1 = pin.pin['num1']
        if(num1 == ''):
            output.popup("Warning！",'禁止提交空内容！')
        else:
            p = open("clipboard.txt",'w')
            p.write(num1)
            p.close()
            pyperclip.copy(num1)
            output.put_markdown(str(time.time()) + "提交成功！\n\n\n\n\nSuccess！" + "\n内容：" + num1)
            output.popup("提交成功！", '提交：' + num1)
    def reads():
        q2 = pyperclip.paste()
        output.popup("获取成功！",'剪切板内容：' + q2)
        output.put_markdown(str(time.time()) + "获取成功！\n\n\n\n\nSuccess！")
    hold()
def start_sv(port):
    start_server(
        tst,
        port=port,
        reconnect_timeout=3000,
        debug=True,
        auto_open_webbrowser=True,
        remote_access=True
    )